package edu.spec.sms.controller;

import edu.spec.sms.model.mental.Rubric;
import edu.spec.sms.service.iface.IRubricService;
import edu.spec.sys.model.dto.MvcReturn;
import org.madgecko.basic.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class RubricController {

    @Inject
    private IRubricService rubricService;

    /**
     * 学生加载题目
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/student/rubric/listByMental")
    public MvcReturn listRubricByMental(HttpSession session){
        Integer mentalId = (Integer) session.getAttribute("mentalId");
        List<Rubric> lr = rubricService.listRubricByMental(mentalId);
        return new MvcReturn(true, lr);
    }

    /**
     *
     * @param mentalId
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/rubric/listByMental")
    public MvcReturn listRubricByMental(Integer mentalId){
        List<Rubric> lr = rubricService.listRubricByMental(mentalId);
        return new MvcReturn(true, lr);
    }

    @RequestMapping(value="/admin/mental/rubric/importIn", method= RequestMethod.POST)
    public void importRubric(Integer mentalId, MultipartFile file, HttpServletRequest req, HttpServletResponse resp){
        try {
            boolean b = rubricService.importIn(mentalId, file, req);
            MvcReturn er = new MvcReturn(b);
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().write(JsonUtil.getInstance().obj2json(er));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
